// pages/coupon_package/coupon_index.js
import {
  wxRequest
} from '../../utils/server.js';
const _wxRequest = new wxRequest();
const app = getApp()

Page({

  /**
   * 页面的初始数据
   */
  data: {
    imgUrl: app.globalData.imgUrl,
    pageNo: 1,
    list: [],
    orgType: '',
    hasNextPage: false,
    keyword: '',
    status: '',
    pageData: {}
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.getList();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    if (wx.getStorageSync('isSent')) {
      this.getList()
      wx.setStorageSync('isSent', false)
    }
  },

  getList: function () {
    this.data.orgType = wx.getStorageSync('userInfo').orgType
    var url = ''
    if (this.data.orgType === 4) {
      url = '/odr/protected/comm/beanCrud/mar.VoucherVO/find'
    } else {
      url = '/odr/protected/voucher/findUserVoucher'
    }
    let params = {
      pageNo: this.data.pageNo || 1,
      pageSize: 10,
      supId: wx.getStorageSync('userInfo').orgId
    }
    _wxRequest.wxTonkenAjax({
      url: app.globalData.apiUrl + url,
      method: 'POST', 
      data: params
    }, (res) => {
      let list = res.list;
      if (this.data.pageNo !== 1) {
        list = this.data.list.concat(list);
      }
      this.setData({
        list: list,
        orgType: this.data.orgType,
        pageNo: res.page.pageNo,
        hasNextPage: res.page.hasNextPage,
      })
    })
  },

  // 跳转至 '生成代金券' 页面
  toCreateCoupon: function () {
    wx.navigateTo({
      url: '/pages/coupon_package/coupon_create/coupon_create',
    })
  },

  toCouponRecord: function () {
    wx.navigateTo({
      url: '/pages/coupon_package/writeoff_record/writeoff_record',
    })
  },

  // 跳转至核销页
  toCouponWriteOff: function (e) {
    const vouId = e.currentTarget.dataset.id;

    wx.setStorageSync('vouId', vouId)
    wx.navigateTo({
      url: '/pages/coupon_package/coupon_writeoff/coupon_writeoff',
    })
  },

  // 跳转至赠送页
  toCouponSend: function (e) {

    const vouId = e.currentTarget.dataset.id;

    wx.setStorageSync('vouId', vouId)
    wx.navigateTo({
      url: '/pages/coupon_package/coupon_send/coupon_send',
    })
  },

  //下拉刷新
  onPullDownRefresh: function () {
    this.refreshData();
    wx.stopPullDownRefresh()
  },

  refreshData: function () {
    this.setData({
      pageNo: 1
    })
    this.getList();
  },

  //触底时，加载更多
  onReachBottom: function () {
    if (this.data.hasNextPage) {
      this.setData({
        pageNo: this.data.pageNo + 1
      });
      this.getList();
    }
  },

  //搜索关键词
  seachInputConfirm: function (e) {
    this.setData({
      pageNo: 1,
      keyword: e.detail.value
    })
    this.getList();
  },

  seachBtn: function () {
    this.setData({
      pageNo: 1,
      keyword: this.data.keyword
    })
    this.getList();
  },

  seachInputVal: function (e) {
    var inputval = e.detail.value;
    this.setData({
      keyword: inputval
    })
  },

  //清空
  clearInput: function () {
    this.setData({
      keyword: ''
    })
    this.getList();
  },

  //********************筛选更多********************
  popupRight: function () {
    this.setData({
      showPopup: true
    })
  },

  popupCancel: function () {
    this.setData({
      showPopup: false
    })
  },

  popupConfirm: function () {
    this.getList();
    this.popupCancel();
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },
})